package com.xnew.utils;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 接口返回数据的工具类
 * @author ChaiXY
 */
public class ResponseUtil {
	public static String responseMsg(String code,String message,Object responseData){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("message", message);
		map.put("data", responseData);
		ObjectMapper mapper = new ObjectMapper();
		String responseJsonStr ;
		try {
			responseJsonStr = mapper.writeValueAsString(map);
			return responseJsonStr;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "结果转换错误";
		}
	}
	
	public static String responseMsg(Object responseData){
		ObjectMapper mapper = new ObjectMapper();
		String responseJsonStr ;
		try {
			responseJsonStr = mapper.writeValueAsString(responseData);
			return responseJsonStr;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "结果转换错误";
		}
	}
	
	public static String responseMsg(String code,String message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("message", message);
		ObjectMapper mapper = new ObjectMapper();
		String responseJsonStr ;
		try {
			responseJsonStr = mapper.writeValueAsString(map);
			return responseJsonStr;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return "结果转换错误";
		}
	}
}
